//
//  CRCommentCell.swift
//  GZWeibo007
//
//  Created by 雷路荣 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class CRCommentCell: UITableViewCell {

    // cell显示内容需要模型
    var status: CZStatus? {
        didSet{
            //设置头像图片
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            //设置昵称
            nameLabel.text = status?.user?.screen_name
            //设置时间
            timeLabel.text = status?.created_at
            //来源
            //sourceLabel.text = status?.source
            //vip等级图标
            vipView.image = status?.user?.mbrankImage
            //认证图片
            verifiedView.image = status?.user?.verifiedImage
            //正文
            contentLabel.text = status?.text
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }
    //添加子控件
    private func prepareUI() {
        //添加控件
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        //contentView.addSubview(sourceLabel)
        contentView.addSubview(vipView)
        contentView.addSubview(verifiedView)
        contentView.addSubview(contentLabel)
        //设置控件约束
        //设置头像约束
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSizeMake(35, 35), offset: CGPointMake(8, 8))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPointMake(8, 0))
        vipView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLabel, size: nil, offset: CGPointMake(8, 0))
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPointMake(8, 0))
        //sourceLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: timeLabel, size: nil, offset: CGPointMake(8, 0))
        verifiedView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSizeMake(17, 17), offset: CGPointMake(8.5, 8.5))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPointMake(0, 8))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 60))
        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 8))
    }
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "avatar")
        let imageView = UIImageView(image: image)
        imageView.sizeToFit()
        imageView.layer.cornerRadius = 17.5
        imageView.clipsToBounds = true
        return imageView
    }()
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
    
    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 9)
    
    /// 微博来源
    //private lazy var sourceLabel: UILabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 9)
    /// 会员等级
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: nil)
    /// 正文
    private lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
        label.numberOfLines = 0
        return label
    }()
}
